package com.app.service.Impl;

import com.app.common.SystemConstant;
import com.app.mapper.UserMapper;
import com.app.model.UserModel;
import com.app.service.UserService;
import com.app.util.CommonHelp;
import com.app.util.GetUUID;
import com.app.util.WechatUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.security.auth.login.LoginException;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private UserMapper userMapper;


    @Override
    public UserModel wxLogin(String code, String mobileCode, HttpServletRequest request) throws LoginException {
        //获取手机号
        String mobile = WechatUtil.getMobile(mobileCode);
        //获取openid
        String openid = WechatUtil.getOpenid(code);

        if (openid == null) {
            throw new LoginException("openid为空");
        }
        //根据openid查询用户信息
        UserModel user = userMapper.getByOpenid(openid);
        if (user == null) {
            //当前是新用户，自动注册
            user = new UserModel();
            user.setUserName("用户" + GetUUID.getUUID(8));
            user.setFaceUrl(SystemConstant.DEFAULT_FACE_URL);//首次登录设置默认头像
            user.setOpenid(openid);
            user.setMobile(mobile);
            user.setCreateTime(CommonHelp.getNowTimestamp());
            user.setUpdateTime(CommonHelp.getNowTimestamp());
            userMapper.insert(user);
            return userMapper.getByOpenid(openid);
        }
        user.setMobile(mobile);
        userMapper.updateById(user);
        UserModel tbUser = new UserModel();
        tbUser.setUserName(user.getUserName());
        tbUser.setOpenid(user.getOpenid());
        tbUser.setMobile(mobile);
        tbUser.setId(user.getId());
        tbUser.setFaceUrl(user.getFaceUrl());
        return tbUser;
    }

    @Override
    public void updateUserInfo(UserModel user) {
        userMapper.updateById(user);
    }

    @Override
    public UserModel updateAvatar(UserModel model) {
        userMapper.updateById(model);
        return userMapper.selectById(model.getId());
    }


    @Override
    public UserModel getUserByOpenid(String mobile) {
        QueryWrapper<UserModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("openid", mobile);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public UserModel selectUserById(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public List<UserModel> selectAll(Integer page, Integer limit, String keyword) {
        return userMapper.selectAll(page, limit, keyword);
    }

    @Override
    public int selectCount(String keyword) {
        return userMapper.selectCountByKeyword(keyword);
    }

    @Override
    public int selectAllCount() {
        return userMapper.selectCount(null);
    }

}
